﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Element.Net.Controls
{
    public class ElementZone : UserControl
    {
        public ElementZone()
        {
            
        }

        #region 属性
        private Color mouseDownCorlor;
        private Color mouseUpCorlor;
        private float radius = 1f;
        private bool topLeft = true;
        private bool topRight = true;
        private bool bottomLeft = true;
        private bool bottomRight = true;
        private string text;
        private bool isDrawBorder = false;

        [Browsable(true)]
        public Color MouseDownCorlor { get => mouseDownCorlor; set => mouseDownCorlor = value; }

        [Browsable(true)]
        public Color MouseUpCorlor { get => mouseUpCorlor; set => mouseUpCorlor = value; }

        public float Radius { get => radius; set => radius = value; }

        public bool TopLeft { get => topLeft; set => topLeft = value; }

        public bool TopRight { get => topRight; set => topRight = value; }

        public bool BottomLeft { get => bottomLeft; set => bottomLeft = value; }

        public bool BottomRight { get => bottomRight; set => bottomRight = value; }

        [Browsable(true)]
        public override string Text { get => text; set => text = value; }

        public bool IsDrawBorder { get => isDrawBorder; set => isDrawBorder = value; }

        /// <summary>
        /// 文本布局
        /// </summary>
        private readonly StringFormat stringFormat = new StringFormat()
        {
            LineAlignment = StringAlignment.Center,
            Alignment = StringAlignment.Center
        };
        #endregion

        #region 事件
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            BackColor = mouseUpCorlor;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            BackColor = mouseDownCorlor;
        }

        /// <summary>
        /// 重绘背景
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(-1, -1, Width, Height);
            DrawRectangle(graphics, rectangle, Color.Transparent, 1f);
        }

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(-1, -1, Width, Height);
            DrawRectangle(graphics, rectangle, BackColor, radius);
            Rectangle rect = new Rectangle(0, 0, Width - 1, Height);
            if (IsDrawBorder) graphics.DrawPath(new Pen(Color.LightGray), GetGraphicsPath(rect, radius));
        }
        #endregion

        #region 方法
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        private GraphicsPath GetGraphicsPath(Rectangle rect, float radius)
        {
            GraphicsPath GraphicsPath = new GraphicsPath();
            // 左上角
            float r = topLeft ? radius : 1f;
            GraphicsPath.AddArc(rect.X, rect.Y, r, r, 180, 90);
            // 右上角
            r = topRight ? radius : 1f;
            GraphicsPath.AddArc(rect.Width - r, rect.Y, r, r, 270, 90);
            // 右下角
            r = bottomRight ? radius : 1f;
            GraphicsPath.AddArc(rect.Width - r, rect.Height - r, r, r, 0, 90);
            // 左下角
            r = bottomLeft ? radius : 1f;
            GraphicsPath.AddArc(rect.X, rect.Height - r, r, r, 90, 90);
            GraphicsPath.CloseAllFigures();
            return GraphicsPath;
        }

        /// <summary>
        /// 高质量绘制矩形
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="rectangle"></param>
        /// <param name="color"></param>
        private void DrawRectangle(Graphics graphics, Rectangle rectangle, Color color, float radius)
        {
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            GraphicsPath graphicsPath = GetGraphicsPath(rectangle, radius);
            SolidBrush solidBrush = new SolidBrush(color);
            graphics.FillPath(solidBrush, graphicsPath);
            rectangle.Y += 4;
            graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectangle, stringFormat);
        }
        #endregion
    }
}
